package com.guaika.reggie.common;


import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author GuaiKa
 * 全局异常类
 */


@Slf4j
@ControllerAdvice(annotations = {RestController.class,Controller.class})
@ResponseBody
public class GlobalExceptionHandler {
    /***
     * 全局异常，重复插入
     * @param e
     * @return
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public ResultMessage<String> exceptionHandler(SQLIntegrityConstraintViolationException e){
        if(e.getMessage().contains(StringChars.DUPLICATE_ENTRY.getStr())){
            String[] err = e.getMessage().split(" ");
            String mes = err[2] + StringChars.EXIT.getStr();
            ResultMessage.error(mes);
        }
        return ResultMessage.error(StringChars.UNKNOWN_ERROR.getStr());
    }

    /***
     * 全局自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public ResultMessage<String> exceptionHandler(CustomException e){
        return ResultMessage.error(e.getMessage());
    }
}
